#include<iostream>

using namespace std;

template<class K>
struct BSTNode
{
	BSTNode<K>* _left;
	BSTNode<K>* _right;

	K _key;

	BSTNode(const K& key)
		:_left(nullptr)
		, _right(nullptr)
		, _key(key)
	{}
};


template<class T>
class BSTree
{
	typedef BSTNode<T> Node;
private:
	void destroytree(Node* root)
	{
		if (root == nullptr)
			return;

		destroytree(root->_left);
		destroytree(root->_right);
		delete root;
	}

	Node* copytree(Node* root)
	{
		if (root == nullptr)
			return nullptr;

		Node* copynode = new Node(root->_key);
		copynode->_left = copytree(root->_left);
		copynode->_right = copytree(root->_right);

		return copynode;
	}

public:
	BSTree() = default;

	BSTree(const BSTree<T>& t)
	{
		_root = copytree(t._root);
	}

	BSTree<T>& operator=(BSTree<T> t)
	{
		swap(t._root, _root);
		return *this;
	}

	~BSTree()
	{
		destroytree(_root);
		_root = nullptr;
	}

	bool insert(const T& key)
	{
		if (_root == nullptr)
		{
			_root = new Node(key);
			return true;
		}

		Node* parent = nullptr;
		Node* cur = _root;

		while (cur)
		{
			if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}

		cur = new Node(key);
		if (parent->_key < key)
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}

		return true;
	}

	bool find(const T& key)
	{
		Node* cur = _root;

		while (cur)
		{
			if (cur->_key < key)
			{
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				cur = cur->_left;
			}
			else
			{
				return true;
			}
		}
		return false;
	}

	void inOrder()
	{
		_inOrder(_root);
		cout << endl;
	}

	bool erase(const T& key)
	{
		Node* parent = nullptr;
		Node* cur = _root;

		while (cur)
		{
			if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				if (cur->_left == nullptr)
				{
					if (cur == _root)
					{
						_root = cur->_right;
					}
					else
					{
						if (cur == parent->_left)
						{
							parent->_left = cur->_right;
						}
						else
						{
							parent->_right = cur->_right;
						}
					}
					delete cur;
				}
				else if (cur->_right == nullptr)
				{
					if (cur == _root)
					{
						_root = cur->_left;
					}
					else
					{
						if (cur == parent->_left)
						{
							parent->_left = cur->_left;
						}
						else
						{
							parent->_right = cur->_left;
						}
					}
					delete cur;
				}
				else
				{
					Node* minparent = cur;
					Node* minright = cur->_right;
					while (minright->_left)
					{
						minparent = minright;
						minright = minright->_left;
					}

					swap(minright->_key, cur->_key);

					if (minright == minparent->_left)
					{
						minparent->_left = minright->_right;
					}
					else
					{
						minparent->_right = minright->_right;
					}

					delete minright;
				}
				return true;
			}
		}
		return false;
	}

	bool eraseR(const T& key)
	{
		return _eraseR(_root, key);
	}

	bool findR(const T& key)
	{
		return _findR(_root, key);
	}

	bool insertR(const T& key)
	{
		return _insertR(_root, key);
	}

private:
	bool _eraseR(Node*& root, const T& key)
	{
		if (root == nullptr)
			return false;

		if (root->_key < key)
		{
			_eraseR(root->_right, key);
		}
		else if (root->_key > key)
		{
			_eraseR(root->_left, key);
		}
		else
		{
			Node* del = root;
			if (root->_left == nullptr)
			{
				root = root->_right;
			}
			else if (root->_right == nullptr)
			{
				root = root->_left;
			}
			else
			{
				Node* minright = root->_right;
				while (minright->_left)
				{
					minright = minright->_left;
				}

				swap(root->_key, minright->_key);
				return _eraseR(root->_right, key);
			}

			delete del;
			return true;
		}
	}

	bool _findR(Node* root, const T& key)
	{
		if (root == nullptr)
			return false;

		if (root->_key < key)
		{
			_findR(root->_right, key);
		}
		else if (root->_key > key)
		{
			_findR(root->_left, key);
		}
		else
		{
			return true;
		}
	}

	bool _insertR(Node*& root, const T& key)
	{
		if (root == nullptr)
		{
			root = new Node(key);
			return true;
		}

		if (root->_key < key)
		{
			_insertR(root->_right, key);
		}
		else if (root->_key > key)
		{
			_insertR(root->_left, key);
		}
		else
		{
			return false;
		}
	}

	void _inOrder(Node* root)
	{
		if (root == nullptr)
			return;

		_inOrder(root->_left);
		cout << root->_key << " ";
		_inOrder(root->_right);
	}

	Node* _root = nullptr;
};